package dao

import (
	"github.com/crack007/go-im/src/common/entity"
	"github.com/crack007/go-im/src/common/util"
	"gorm.io/gorm"
)

type contactDao struct {
	Db *gorm.DB
}

func (d *contactDao) List(id uint32, page uint32, size uint32) []*entity.ContactEntity {
	var list []*entity.ContactEntity
	offset := (page - 1) * size
	d.Db.Where("is_delete=0 and user_id=?", id).Limit(int(size)).Offset(int(offset)).Find(&list)
	return list
}

func (d *contactDao) Save(contactEntity *entity.ContactEntity) {
	if contactEntity.ID > 0 {
		contactEntity.UpdatedAt = util.GetDatetime()
	}
	d.Db.Save(contactEntity)
}

func (d *contactDao) Delete(contactEntity *entity.ContactEntity) {
	contactEntity.IsDelete = true
	d.Db.Delete(contactEntity)
}
